home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / Animator.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-16  |  4.2 KB  |  207 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.net.URL;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. import symantec.beans.Beans;
  13.  
  14. public class Animator extends Canvas implements Runnable {
  15.    protected int delay = 500;
  16.    protected int numLoops = 1;
  17.    protected boolean forever = true;
  18.    protected Vector images = new Vector();
  19.    protected Image currentImage = null;
  20.    protected Thread displayThread;
  21.    protected int maxWidth = 0;
  22.    protected int maxHeight = 0;
  23.    protected boolean clearFrame = false;
  24.    protected boolean previewMode = false;
  25.  
  26.    public void setDelay(int var1) {
  27.       this.delay = var1;
  28.    }
  29.  
  30.    public int getDelay() {
  31.       return this.delay;
  32.    }
  33.  
  34.    public void setNumLoops(int var1) {
  35.       this.numLoops = var1;
  36.    }
  37.  
  38.    public int getNumLoops() {
  39.       return this.numLoops;
  40.    }
  41.  
  42.    public void setRepeatMode(boolean var1) {
  43.       this.forever = var1;
  44.    }
  45.  
  46.    public boolean getRepeatMode() {
  47.       return this.forever;
  48.    }
  49.  
  50.    void imageLoadWait(Image var1) {
  51.       MediaTracker var2 = new MediaTracker(this);
  52.       var2.addImage(var1, 0);
  53.  
  54.       try {
  55.          var2.waitForAll();
  56.       } catch (InterruptedException var4) {
  57.       }
  58.  
  59.       int var3;
  60.       if ((var3 = var1.getWidth(this)) > this.maxWidth) {
  61.          this.maxWidth = var3;
  62.       }
  63.  
  64.       if ((var3 = var1.getHeight(this)) > this.maxHeight) {
  65.          this.maxHeight = var3;
  66.       }
  67.  
  68.    }
  69.  
  70.    public synchronized void addImage(URL var1) {
  71.       Image var2 = ((Component)this).getToolkit().getImage(var1);
  72.       boolean var3 = this.images.size() == 0 || this.previewMode || !Beans.isDesignTime();
  73.       if (var3) {
  74.          this.imageLoadWait(var2);
  75.       }
  76.  
  77.       this.images.addElement(new AnimatorImage(var1, var2, var3));
  78.    }
  79.  
  80.    public synchronized void setImageList(URL[] var1) {
  81.       boolean var2 = this.displayThread != null;
  82.       if (var2) {
  83.          this.stopAnimation();
  84.          this.displayThread = null;
  85.       }
  86.  
  87.       this.currentImage = null;
  88.       this.images = new Vector();
  89.  
  90.       for(int var3 = 0; var3 < var1.length; ++var3) {
  91.          this.addImage(var1[var3]);
  92.       }
  93.  
  94.       if (!var2 && !this.previewMode && Beans.isDesignTime()) {
  95.          if (var1.length > 0) {
  96.             this.currentImage = ((AnimatorImage)this.images.elementAt(0)).image;
  97.          }
  98.       } else {
  99.          this.startAnimation();
  100.       }
  101.  
  102.       ((Component)this).repaint();
  103.    }
  104.  
  105.    public synchronized URL[] getImageList() {
  106.       URL[] var1 = new URL[this.images.size()];
  107.       byte var2 = 0;
  108.  
  109.       for(Enumeration var3 = this.images.elements(); var3.hasMoreElements(); var1[var2] = ((AnimatorImage)var3.nextElement()).url) {
  110.       }
  111.  
  112.       return var1;
  113.    }
  114.  
  115.    public void setClearFrame(boolean var1) {
  116.       this.clearFrame = var1;
  117.    }
  118.  
  119.    public boolean getClearFrame() {
  120.       return this.clearFrame;
  121.    }
  122.  
  123.    public void startAnimation() {
  124.       if (this.displayThread == null) {
  125.          this.displayThread = new Thread(this);
  126.          this.displayThread.start();
  127.       }
  128.  
  129.    }
  130.  
  131.    public void stopAnimation() {
  132.       if (this.displayThread != null) {
  133.          this.displayThread.stop();
  134.          this.displayThread = null;
  135.       }
  136.  
  137.    }
  138.  
  139.    public void setPreviewMode(boolean var1) {
  140.       if (Beans.isDesignTime()) {
  141.          this.previewMode = var1;
  142.          if (this.previewMode) {
  143.             this.startAnimation();
  144.             return;
  145.          }
  146.  
  147.          this.stopAnimation();
  148.       }
  149.  
  150.    }
  151.  
  152.    public boolean getPreviewMode() {
  153.       return this.previewMode;
  154.    }
  155.  
  156.    public synchronized void run() {
  157.       for(int var1 = 0; var1 < this.numLoops || this.forever; ++var1) {
  158.          for(int var2 = 0; var2 < this.images.size(); ++var2) {
  159.             synchronized(this){}
  160.  
  161.             try {
  162.                try {
  163.                   this.wait((long)this.delay);
  164.                } catch (InterruptedException var8) {
  165.                }
  166.  
  167.                AnimatorImage var5 = (AnimatorImage)this.images.elementAt(var2);
  168.                if (!var5.loaded) {
  169.                   this.imageLoadWait(var5.image);
  170.                   this.images.setElementAt(new AnimatorImage(var5.url, var5.image, true), var2);
  171.                }
  172.  
  173.                this.currentImage = var5.image;
  174.             } catch (Throwable var9) {
  175.                throw var9;
  176.             }
  177.  
  178.             ((Component)this).repaint();
  179.          }
  180.       }
  181.  
  182.    }
  183.  
  184.    public synchronized void paint(Graphics var1) {
  185.       if (this.currentImage != null) {
  186.          var1.drawImage(this.currentImage, 0, 0, this);
  187.       }
  188.  
  189.    }
  190.  
  191.    public void update(Graphics var1) {
  192.       if (this.clearFrame) {
  193.          super.update(var1);
  194.       } else {
  195.          this.paint(var1);
  196.       }
  197.    }
  198.  
  199.    public Dimension preferredSize() {
  200.       return this.images != null && this.images.size() != 0 ? new Dimension(this.maxWidth, this.maxHeight) : new Dimension(10, 10);
  201.    }
  202.  
  203.    public Dimension minimumSize() {
  204.       return this.preferredSize();
  205.    }
  206. }
  207.